# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/2/14
# @File   : R24_IC_InquiriesCntCheck.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:申请人近1个月内贷款、信用卡审批查询机构数校验
code:R24_IC_InquiriesCntCheck_2D
tableName:
columnName:
groups:个人征信校验
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import pandas as pd
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None:
            return [{}]
        elif table == "":
            return ""
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)
    if occurTimes == 1:
        if table == "":
            return ""
        elif table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)


def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def R24_IC_InquiriesCntCheck():
    data = IC_DSJ_2D['data']
    # 查询记录概要
    PQO = getTable(data, 'PQO', 1)
    if PQO is None or PQO == "":
        return u'缺失值'
    else:
        # 查询记录概要信息单元
        PC05 = getTable(PQO[0], 'PC05', 1)
        if PC05 is None or PC05 == "":
            return u'缺失值'
        else:
            # 查询记录汇总信息段
            PC05B = getTable(PC05[0], 'PC05B', 1)
            if PC05B is None or PC05B == "":
                return u'缺失值'
            else:
                # 最近 1 个月内的查询机构数（贷款审批）
                PC05BS01 = PC05B[0].get('PC05BS01')
                # 最近 1 个月内的查询机构数（信用卡审批）
                PC05BS02 = PC05B[0].get('PC05BS02')
                if isfloat(PC05BS01) is False and isfloat(PC05BS02) is False:
                    return u'缺失值'
                else:
                    return (float(PC05BS01) if isfloat(PC05BS01) else 0) + (float(PC05BS02) if isfloat(PC05BS02) else 0)


result = R24_IC_InquiriesCntCheck()
